package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.GameClient;
import model.GameTile;

@SuppressWarnings("serial")
public class ClientGameBoard extends JPanel
{
	private GameClient gameClient;

	ImageIcon monsterIcon = new ImageIcon("images/monster.png", "Monster");
	JLabel monsterLabel = new JLabel(monsterIcon);

	ImageIcon clientIcon = new ImageIcon("images/normal.png", "Normal");
	JLabel clientLabel = new JLabel(clientIcon);

	ImageIcon clientIcon1 = new ImageIcon("images/other.png", "Other");
	JLabel clientLabel1 = new JLabel(clientIcon1);

	public ClientGameBoard(GameClient gameClient)
	{
		super();
		this.gameClient = gameClient;

		setStage();
	}

	public void setStage()
	{
		GridBagConstraints gbc = new GridBagConstraints();

		/*
		 * The 9x9 Square Grid, and the individual squares in it
		 */

		final int width = 9, height = 9;

		JLabel[][] label = new JLabel[width][height];

		// int count = 1;
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				label[i][j] = new JLabel();
				label[i][j].setBorder(BorderFactory
						.createLineBorder(Color.BLACK));
				label[i][j].setPreferredSize(new Dimension(80, 80));
				label[i][j].setOpaque(true);

				// Determining if square is coloured White OR Black
				boolean isStage = true;
				if ((i > 0 && i < 8) && i != 4)
					if ((j > 0 && j < 8) && j != 4)
						isStage = false;

				if (isStage == false)
				{
					label[i][j].setBackground(Color.BLACK);
				}
				else
				{
					label[i][j].setBackground(Color.WHITE);

				}

				if (gameClient.isStarted())
				{

					switch (gameClient.getStart())
					{
					case 1:
						label[0][0] = clientLabel;
						gameClient.setCP(0, 0);
						break;
					case 2:
						label[0][8] = clientLabel;
						gameClient.setCP(0, 8);
						break;
					case 3:
						label[8][0] = clientLabel;
						gameClient.setCP(8, 0);
						break;
					case 4:
						label[8][8] = clientLabel;
						gameClient.setCP(8, 8);
						break;
					default:
						break;
					}

				}
				else
				{
					if (i == 0 && j == 1)
						label[0][1].setText("       M ");
					if (i == 0 && j == 2)
						label[0][2].setText("       O ");
					if (i == 0 && j == 3)
						label[0][3].setText("       N ");
					if (i == 0 && j == 4)
						label[0][4].setText("       S ");
					if (i == 0 && j == 5)
						label[0][5].setText("       T ");
					if (i == 0 && j == 6)
						label[0][6].setText("       E ");
					if (i == 0 && j == 7)
						label[0][7].setText("       R ");

					if (i == 4 && j == 1)
						label[4][1].setText("       M ");
					if (i == 4 && j == 2)
						label[4][2].setText("       U ");
					if (i == 4 && j == 3)
						label[4][3].setText("       N ");
					if (i == 4 && j == 4)
						label[4][4].setText("       C ");
					if (i == 4 && j == 5)
						label[4][5].setText("       H ");
					if (i == 4 && j == 6)
						label[4][6].setText("       I ");
					if (i == 4 && j == 7)
						label[4][7].setText("       E ");
					if (i == 4 && j == 8)
						label[4][8].setText("       S ");
				}

				/* END GRID UI TEST */

				gbc.gridx = j + 1;
				gbc.gridy = i + 1;
				add(label[i][j], gbc);
			}
		}
		System.out.println("Stage has been set");
	}

	@SuppressWarnings("unchecked")
	public void refreshStage(int xCoord, int yCoord) throws IOException,
			ClassNotFoundException
	{
		GridBagConstraints gbc = new GridBagConstraints();

		/*
		 * The 9x9 Square Grid, and the individual squares in it
		 */

		final int width = 9, height = 9;

		JLabel[][] label = new JLabel[width][height];

		ObjectInputStream oin = null;
		ArrayList<GameTile> gameTiles = null;

		// int count = 1;
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				label[i][j] = new JLabel();
				label[i][j].setBorder(BorderFactory
						.createLineBorder(Color.BLACK));
				label[i][j].setPreferredSize(new Dimension(80, 80));
				label[i][j].setOpaque(true);

				// Determining if square is coloured White OR Black
				boolean isStage = true;
				if ((i > 0 && i < 8) && i != 4)
					if ((j > 0 && j < 8) && j != 4)
						isStage = false;

				if (isStage == false)
				{
					label[i][j].setBackground(Color.BLACK);
				}
				else
				{
					label[i][j].setBackground(Color.WHITE);
					label[i][j].setText(i + ", " + j);
				}

				if (i == 8 && j == 8)
				{
					gameClient.getOutputToServer().write(12);
					gameClient.getOutputToServer().flush();

					try
					{
						System.out.println("Create inputStream");
						// give it a random name for different threads testing
						// on same computer
						boolean keepGoing = true;
						while (keepGoing)
						{
							try
							{
								oin = new ObjectInputStream(
										new FileInputStream("loltiles.dat"));

								System.out
										.println("Finish creating inputStream");
								gameTiles = new ArrayList<GameTile>();
								System.out.println("Reading inputStream");

								gameTiles = (ArrayList<GameTile>) oin
										.readObject();
								System.out
										.println("inputStream to array list successful");
								keepGoing = false;
							}
							catch (EOFException e)
							{
							}
						}

						oin.close();
					}
					catch (FileNotFoundException e)
					{
						System.out.println("FILE NOT FOUND");
					}
				}
				label[xCoord][yCoord] = clientLabel;
				System.out.println("YOU ARE AT " + xCoord + ":" + yCoord);
				gameClient.setCP(xCoord, yCoord);
				/* END GRID UI TEST */
			}
		}

		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				boolean tiled = false;
				for (GameTile gT : gameTiles)
				{
					if ((gT.getxCoord() == i && gT.getyCoord() == j)
							&& (i != xCoord && j != yCoord))
					{
						label[gT.getxCoord()][gT.getyCoord()] = clientLabel1;
						System.out.println("OTHER PLAYER AT: " + gT.getxCoord()
								+ ":" + gT.getyCoord());

						add(label[gT.getxCoord()][gT.getyCoord()], gbc);
						tiled = true;
					}
				}

				gbc.gridx = i;
				gbc.gridy = j;
				if (!tiled)
					add(label[i][j], gbc);
			}
		}

		System.out.println("Stage has been refreshed");
	}

}
